package cn.puge.usercenter.service.impl;

import cn.puge.usercenter.entity.PugeUserLogin;
import cn.puge.usercenter.mapper.PugeUserLoginMapper;
import cn.puge.usercenter.service.PugeUserLoginService;
import cn.puge.usercenter.utils.SaltUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LIJW
 * @since 2020-09-28
 */
@Service("userLoginService")
public class PugeUserLoginServiceImpl extends ServiceImpl<PugeUserLoginMapper, PugeUserLogin> implements PugeUserLoginService {


    /**
     * 从数据库中查询登录详细信息 供shiro验证
     * @param username
     * @return
     */
    @Override
    public PugeUserLogin getUserLogin(String username) {

        QueryWrapper<PugeUserLogin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        PugeUserLogin pugeUserLogin = baseMapper.selectOne(queryWrapper);

        return pugeUserLogin;
    }

    /**
     * 注册账号
     * @param pugeUserLogin
     */
    @Override
    public void UserRegister(PugeUserLogin pugeUserLogin) {

        String salt = SaltUtils.getSalt(4);
        pugeUserLogin.setSalt(salt);
        //没问密码进行MD5+salt+hash散列
        Md5Hash md5Hash = new Md5Hash(pugeUserLogin.getPassword(), salt, 1024);
        pugeUserLogin.setPassword(md5Hash.toHex());
        baseMapper.insert(pugeUserLogin);
    }
}
